home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / gfx / show / mpeg_src_play201.lha / org20 / ordered.c < prev    next >
C/C++ Source or Header  |  2001-02-02  |  8KB  |  287 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21. /* This file contains C code to implement an ordered dither. */
  22.  
  23. #include "video.h"
  24. #include "proto.h"
  25. #include "dither.h"
  26.  
  27. #define DITH_SIZE 16
  28.  
  29.  
  30. /* Structures used to implement hybrid ordered dither/floyd-steinberg
  31.    dither algorithm.
  32. */
  33.  
  34. static unsigned char *l_darrays[DITH_SIZE];
  35. static unsigned char *cr_darrays[DITH_SIZE];
  36. static unsigned char *cb_darrays[DITH_SIZE];
  37.  
  38. /*
  39.  *--------------------------------------------------------------
  40.  *
  41.  *  InitOrderedDither--
  42.  *
  43.  *    Structures intialized for ordered dithering. 
  44.  *
  45.  * Results:
  46.  *    None.
  47.  *
  48.  * Side effects:
  49.  *      None.
  50.  *
  51.  *--------------------------------------------------------------
  52.  */
  53.  
  54. void
  55. InitOrderedDither()
  56. {
  57.   int i, j, k, err_range, threshval;
  58.   unsigned char *lmark, *cmark;
  59.  
  60.   for (i=0; i<DITH_SIZE; i++) {
  61.     lmark = l_darrays[i] = (unsigned char *) malloc(256);
  62.  
  63.     for (j=0; j<lum_values[0]; j++) {
  64.       *lmark++ = 0;
  65.     }
  66.  
  67.     for (j=0; j<(LUM_RANGE-1); j++) {
  68.       err_range = lum_values[j+1] - lum_values[j];
  69.       threshval = ((i * err_range) / DITH_SIZE)+lum_values[j];
  70.  
  71.       for (k=lum_values[j]; k<lum_values[j+1]; k++) {
  72.     if (k > threshval) *lmark++ = ((j+1) * (CR_RANGE * CB_RANGE));
  73.     else *lmark++ = (j * (CR_RANGE * CB_RANGE));
  74.       }
  75.     }
  76.  
  77.     for (j=lum_values[LUM_RANGE-1]; j<256; j++) {
  78.       *lmark++ = (LUM_RANGE-1)*(CR_RANGE * CB_RANGE);
  79.     }
  80.   }
  81.  
  82.   for (i=0; i<DITH_SIZE; i++) {
  83.     cmark = cr_darrays[i] = (unsigned char *) malloc(256);
  84.  
  85.     for (j=0; j<cr_values[0]; j++) {
  86.       *cmark++ = 0;
  87.     }
  88.  
  89.     for (j=0; j<(CR_RANGE-1); j++) {
  90.       err_range = cr_values[j+1] - cr_values[j];
  91.       threshval = ((i * err_range) / DITH_SIZE)+cr_values[j];
  92.  
  93.       for (k=cr_values[j]; k<cr_values[j+1]; k++) {
  94.     if (k > threshval) *cmark++ = ((j+1) * CB_RANGE);
  95.     else *cmark++ = (j * CB_RANGE);
  96.       }
  97.     }
  98.  
  99.     for (j=cr_values[CR_RANGE-1]; j<256; j++) {
  100.       *cmark++ = (CR_RANGE-1)*(CB_RANGE);
  101.     }
  102.   }
  103.  
  104.   for (i=0; i<DITH_SIZE; i++) {
  105.     cmark = cb_darrays[i] = (unsigned char *) malloc(256);
  106.  
  107.     for (j=0; j<cb_values[0]; j++) {
  108.       *cmark++ = 0;
  109.     }
  110.  
  111.     for (j=0; j<(CB_RANGE-1); j++) {
  112.       err_range = cb_values[j+1] - cb_values[j];
  113.       threshval = ((i * err_range) / DITH_SIZE)+cb_values[j];
  114.  
  115.       for (k=cb_values[j]; k<cb_values[j+1]; k++) {
  116.     if (k > threshval) *cmark++ = j+1;
  117.     else *cmark++ = j;
  118.       }
  119.     }
  120.  
  121.     for (j=cb_values[CB_RANGE-1]; j<256; j++) {
  122.       *cmark++ = CB_RANGE-1;
  123.     }
  124.   }
  125. }
  126.  
  127. /*
  128.  *--------------------------------------------------------------
  129.  *
  130.  * OrderedDitherImage --
  131.  *
  132.  *    Dithers an image using an ordered dither.
  133.  *    Assumptions made:
  134.  *      1) The color space is allocated y:cr:cb = 8:4:4
  135.  *      2) The spatial resolution of y:cr:cb is 4:1:1
  136.  *      The channels are dithered based on the standard
  137.  *      ordered dither pattern for a 4x4 area. 
  138.  *
  139.  * Results:
  140.  *    None.
  141.  *
  142.  * Side effects:
  143.  *    None.
  144.  *
  145.  *--------------------------------------------------------------
  146.  */
  147. void
  148. OrderedDitherImage (lum, cr, cb, out, h, w)
  149.     unsigned char *lum;
  150.     unsigned char *cr;
  151.     unsigned char *cb;
  152.     unsigned char *out;
  153.     int w, h;
  154. {
  155.   unsigned char *l, *r, *b, *o1, *o2;
  156.   unsigned char *l2;
  157.   unsigned char L, R, B;
  158.   int i, j;
  159.  
  160.   l = lum;
  161.   l2 = lum+w;
  162.   r = cr;
  163.   b = cb;
  164.   o1 = out;
  165.   o2 = out+w;
  166.  
  167.   for (i=0; i<h; i+=4) {
  168.  
  169.     for (j=0; j<w; j+=8) {
  170.  
  171.       R = r[0]; B = b[0];
  172.  
  173.       L = l[0];
  174.       o1[0] = pixel[(l_darrays[0][L] + cr_darrays[0][R] + cb_darrays[0][B])];
  175.       L = l[1];
  176.       o1[1] = pixel[(l_darrays[8][L] + cr_darrays[8][R] + cb_darrays[8][B])];
  177.       L = l2[0];
  178.       o2[0] = pixel[(l_darrays[12][L] + cr_darrays[12][R] + cb_darrays[12][B])];
  179.       L = l2[1];
  180.       o2[1] = pixel[(l_darrays[4][L] + cr_darrays[4][R] + cb_darrays[4][B])];
  181.  
  182.       R = r[1]; B = b[1];
  183.  
  184.       L = l[2];
  185.       o1[2] = pixel[(l_darrays[2][L] + cr_darrays[2][R] + cb_darrays[2][B])];
  186.       L = l[3];
  187.       o1[3] = pixel[(l_darrays[10][L] + cr_darrays[10][R] + cb_darrays[10][B])];
  188.       L = l2[2];
  189.       o2[2] = pixel[(l_darrays[14][L] + cr_darrays[14][R] + cb_darrays[14][B])];
  190.       L = l2[3];
  191.       o2[3] = pixel[(l_darrays[6][L] + cr_darrays[6][R] + cb_darrays[6][B])];
  192.  
  193.       R = r[2]; B = b[2];
  194.  
  195.       L = l[4];
  196.       o1[4] = pixel[(l_darrays[0][L] + cr_darrays[0][R] + cb_darrays[0][B])];
  197.       L = l[5];
  198.       o1[5] = pixel[(l_darrays[8][L] + cr_darrays[8][R] + cb_darrays[8][B])];
  199.       L = l2[4];
  200.       o2[4] = pixel[(l_darrays[12][L] + cr_darrays[12][R] + cb_darrays[12][B])];
  201.       L = l2[5];
  202.       o2[5] = pixel[(l_darrays[4][L] + cr_darrays[4][R] + cb_darrays[4][B])];
  203.  
  204.       R = r[3]; B = b[3];
  205.  
  206.       L = l[6];
  207.       o1[6] = pixel[(l_darrays[2][L] + cr_darrays[2][R] + cb_darrays[2][B])];
  208.       L = l[7];
  209.       o1[7] = pixel[(l_darrays[10][L] + cr_darrays[10][R] + cb_darrays[10][B])];
  210.       L = l2[6];
  211.       o2[6] = pixel[(l_darrays[14][L] + cr_darrays[14][R] + cb_darrays[14][B])];
  212.       L = l2[7];
  213.       o2[7] = pixel[(l_darrays[6][L] + cr_darrays[6][R] + cb_darrays[6][B])];
  214.  
  215.       l += 8;
  216.       l2 += 8;
  217.       r += 4;
  218.       b += 4;
  219.       o1 += 8;
  220.       o2 += 8;
  221.     }
  222.  
  223.     l += w; l2 += w;
  224.     o1 += w; o2 += w;
  225.  
  226.     for (j=0; j<w; j+=8) {
  227.  
  228.       R = r[0]; B = b[0];
  229.  
  230.       L = l[0];
  231.       o1[0] = pixel[(l_darrays[3][L] + cr_darrays[3][R] + cb_darrays[3][B])];
  232.       L = l[1];
  233.       o1[1] = pixel[(l_darrays[11][L] + cr_darrays[11][R] + cb_darrays[11][B])];
  234.       L = l2[0];
  235.       o2[0] = pixel[(l_darrays[15][L] + cr_darrays[15][R] + cb_darrays[15][B])];
  236.       L = l2[1];
  237.       o2[1] = pixel[(l_darrays[7][L] + cr_darrays[7][R] + cb_darrays[7][B])];
  238.  
  239.       R = r[1]; B = b[1];
  240.  
  241.       L = l[2];
  242.       o1[2] = pixel[(l_darrays[1][L] + cr_darrays[1][R] + cb_darrays[1][B])];
  243.       L = l[3];
  244.       o1[3] = pixel[(l_darrays[9][L] + cr_darrays[9][R] + cb_darrays[9][B])];
  245.       L = l2[2];
  246.       o2[2] = pixel[(l_darrays[13][L] + cr_darrays[13][R] + cb_darrays[13][B])];
  247.       L = l2[3];
  248.       o2[3] = pixel[(l_darrays[5][L] + cr_darrays[5][R] + cb_darrays[5][B])];
  249.  
  250.       R = r[2]; B = b[2];
  251.  
  252.       L = l[4];
  253.       o1[4] = pixel[(l_darrays[3][L] + cr_darrays[3][R] + cb_darrays[3][B])];
  254.       L = l[5];
  255.       o1[5] = pixel[(l_darrays[11][L] + cr_darrays[11][R] + cb_darrays[11][B])];
  256.       L = l2[4];
  257.       o2[4] = pixel[(l_darrays[15][L] + cr_darrays[15][R] + cb_darrays[15][B])];
  258.       L = l2[5];
  259.       o2[5] = pixel[(l_darrays[7][L] + cr_darrays[7][R] + cb_darrays[7][B])];
  260.  
  261.       R = r[3]; B = b[3];
  262.  
  263.       L = l[6];
  264.       o1[6] = pixel[(l_darrays[1][L] + cr_darrays[1][R] + cb_darrays[1][B])];
  265.       L = l[7];
  266.       o1[7] = pixel[(l_darrays[9][L] + cr_darrays[9][R] + cb_darrays[9][B])];
  267.       L = l2[6];
  268.       o2[6] = pixel[(l_darrays[13][L] + cr_darrays[13][R] + cb_darrays[13][B])];
  269.       L = l2[7];
  270.       o2[7] = pixel[(l_darrays[5][L] + cr_darrays[5][R] + cb_darrays[5][B])];
  271.  
  272.       l += 8;
  273.       l2 += 8;
  274.       r += 4;
  275.       b += 4;
  276.       o1 += 8;
  277.       o2 += 8;
  278.     }
  279.  
  280.     l += w; l2 += w;
  281.     o1 += w; o2 += w;
  282.   }
  283. }
  284.  
  285.  
  286.   
  287.